<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属学院ID" prop="collegeId">
        <el-input
          v-model="queryParams.collegeId"
          placeholder="请输入所属学院ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级
      学科代码" prop="firstSubjectCode">
        <el-input
          v-model="queryParams.firstSubjectCode"
          placeholder="请输入一级学科代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级学科名称" prop="firstSubjectName">
        <el-input
          v-model="queryParams.firstSubjectName"
          placeholder="请输入一级学科名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级学科分类" prop="subjectCategory">
        <el-select v-model="queryParams.subjectCategory" placeholder="请选择一级学科分类" clearable>
          <el-option
            v-for="dict in first_subject_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科负责人id" prop="leaderId">
        <el-input
          v-model="queryParams.leaderId"
          placeholder="请输入学科负责人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="博士授予权(1:有 0:无)" prop="hasDoctorGrant">
        <el-select v-model="queryParams.hasDoctorGrant" placeholder="请选择博士授予权(1:有 0:无)" clearable>
          <el-option
            v-for="dict in doctor_grant"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="硕士授予权(1:有 0:无)" prop="hasMasterGrant">
        <el-select v-model="queryParams.hasMasterGrant" placeholder="请选择硕士授予权(1:有 0:无)" clearable>
          <el-option
            v-for="dict in master_grant"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重点学科类型" prop="keySubjectType">
        <el-select v-model="queryParams.keySubjectType" placeholder="请选择重点学科类型" clearable>
          <el-option
            v-for="dict in key_discipline_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科评估结果(如A+,B-)" prop="evaluationResult">
        <el-select v-model="queryParams.evaluationResult" placeholder="请选择学科评估结果(如A+,B-)" clearable>
          <el-option
            v-for="dict in evaluation_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科设立年份" prop="establishYear">
        <el-date-picker clearable
          v-model="queryParams.establishYear"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择学科设立年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学科状态：1(启用)，0" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择学科状态：1(启用)，0" clearable>
          <el-option
            v-for="dict in use_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['college:firstSubject:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['college:firstSubject:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['college:firstSubject:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['college:firstSubject:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="firstSubjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" />
      <el-table-column label="所属学院ID" align="center" prop="collegeId" />
      <el-table-column label="一级学科代码" align="center" prop="firstSubjectCode" />
      <el-table-column label="一级学科名称" align="center" prop="firstSubjectName" />
      <el-table-column label="一级学科英文名称" align="center" prop="subjectNameEnglish" />
      <el-table-column label="一级学科分类" align="center" prop="subjectCategory">
        <template #default="scope">
          <dict-tag :options="first_subject_category" :value="scope.row.subjectCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="学科负责人id" align="center" prop="leaderId" />
      <el-table-column label="附件存储路径" align="center" prop="attachmentPath" />
      <el-table-column label="学科简介" align="center" prop="description" />
      <el-table-column label="博士授予权(1:有 0:无)" align="center" prop="hasDoctorGrant">
        <template #default="scope">
          <dict-tag :options="doctor_grant" :value="scope.row.hasDoctorGrant"/>
        </template>
      </el-table-column>
      <el-table-column label="硕士授予权(1:有 0:无)" align="center" prop="hasMasterGrant">
        <template #default="scope">
          <dict-tag :options="master_grant" :value="scope.row.hasMasterGrant"/>
        </template>
      </el-table-column>
      <el-table-column label="重点学科类型" align="center" prop="keySubjectType">
        <template #default="scope">
          <dict-tag :options="key_discipline_type" :value="scope.row.keySubjectType"/>
        </template>
      </el-table-column>
      <el-table-column label="学科评估结果(如A+,B-)" align="center" prop="evaluationResult">
        <template #default="scope">
          <dict-tag :options="evaluation_level" :value="scope.row.evaluationResult"/>
        </template>
      </el-table-column>
      <el-table-column label="学科设立年份" align="center" prop="establishYear" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.establishYear, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学科状态：1(启用)，0" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="use_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="版本号(乐观锁)" align="center" prop="version" />
      <el-table-column label="创建人id" align="center" prop="createId" />
      <el-table-column label="更新人id" align="center" prop="updateId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['college:firstSubject:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['college:firstSubject:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改一级学科管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="firstSubjectRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属学院ID" prop="collegeId">
          <el-input v-model="form.collegeId" placeholder="请输入所属学院ID" />
        </el-form-item>
        <el-form-item label="一级学科代码" prop="firstSubjectCode">
          <el-input v-model="form.firstSubjectCode" placeholder="请输入一级学科代码" />
        </el-form-item>
        <el-form-item label="一级学科名称" prop="firstSubjectName">
          <el-input v-model="form.firstSubjectName" placeholder="请输入一级学科名称" />
        </el-form-item>
        <el-form-item label="一级学科英文名称" prop="subjectNameEnglish">
          <el-input v-model="form.subjectNameEnglish" placeholder="请输入一级学科英文名称" />
        </el-form-item>
        <el-form-item label="一级学科分类" prop="subjectCategory">
          <el-select v-model="form.subjectCategory" placeholder="请选择一级学科分类">
            <el-option
              v-for="dict in first_subject_category"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科负责人id" prop="leaderId">
          <el-input v-model="form.leaderId" placeholder="请输入学科负责人id" />
        </el-form-item>
        <el-form-item label="附件存储路径" prop="attachmentPath">
          <file-upload v-model="form.attachmentPath"/>
        </el-form-item>
        <el-form-item label="学科简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="博士授予权(1:有 0:无)" prop="hasDoctorGrant">
          <el-select v-model="form.hasDoctorGrant" placeholder="请选择博士授予权(1:有 0:无)">
            <el-option
              v-for="dict in doctor_grant"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="硕士授予权(1:有 0:无)" prop="hasMasterGrant">
          <el-select v-model="form.hasMasterGrant" placeholder="请选择硕士授予权(1:有 0:无)">
            <el-option
              v-for="dict in master_grant"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重点学科类型" prop="keySubjectType">
          <el-select v-model="form.keySubjectType" placeholder="请选择重点学科类型">
            <el-option
              v-for="dict in key_discipline_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科评估结果(如A+,B-)" prop="evaluationResult">
          <el-select v-model="form.evaluationResult" placeholder="请选择学科评估结果(如A+,B-)">
            <el-option
              v-for="dict in evaluation_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科设立年份" prop="establishYear">
          <el-date-picker clearable
            v-model="form.establishYear"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择学科设立年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学科状态：1(启用)，0" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in use_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="创建人id" prop="createId">
          <el-input v-model="form.createId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="更新人id" prop="updateId">
          <el-input v-model="form.updateId" placeholder="请输入更新人id" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FirstSubject">
import { listFirstSubject, getFirstSubject, delFirstSubject, addFirstSubject, updateFirstSubject } from "@/api/college/firstSubject";

const { proxy } = getCurrentInstance();
const { evaluation_level, key_discipline_type, master_grant, doctor_grant, first_subject_category, use_status } = proxy.useDict('evaluation_level', 'key_discipline_type', 'master_grant', 'doctor_grant', 'first_subject_category', 'use_status');

const firstSubjectList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    collegeId: null,
    firstSubjectCode: null,
    firstSubjectName: null,
    subjectCategory: null,
    leaderId: null,
    hasDoctorGrant: null,
    hasMasterGrant: null,
    keySubjectType: null,
    evaluationResult: null,
    establishYear: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询一级学科管理列表 */
function getList() {
  loading.value = true;
  listFirstSubject(queryParams.value).then(response => {
    firstSubjectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    collegeId: null,
    firstSubjectCode: null,
    firstSubjectName: null,
    subjectNameEnglish: null,
    subjectCategory: null,
    leaderId: null,
    attachmentPath: null,
    description: null,
    hasDoctorGrant: null,
    hasMasterGrant: null,
    keySubjectType: null,
    evaluationResult: null,
    establishYear: null,
    status: null,
    version: null,
    createId: null,
    updateId: null,
    createTime: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("firstSubjectRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加一级学科管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFirstSubject(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改一级学科管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["firstSubjectRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFirstSubject(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFirstSubject(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除一级学科管理编号为"' + _ids + '"的数据项？').then(function() {
    return delFirstSubject(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('college/firstSubject/export', {
    ...queryParams.value
  }, `firstSubject_${new Date().getTime()}.xlsx`)
}

getList();
</script>
